/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.utils;

import javafx.scene.Cursor;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;

/**
 * 调整窗体大小
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public final class ResizeWindowUtils {
    private ResizeWindowUtils(){}

    private static boolean isRight;
    private static boolean isBottomRight;
    private static boolean isBottom;
    private final static int RESIZE_WIDTH = 10;
    private final static double MIN_WIDTH = 1024;
    private final static double MIN_HEIGHT = 600;


    public static void appendResizeFunc(Stage stage, BorderPane root) {
        root.setOnMouseMoved((MouseEvent event) -> onMouseMoved(event,stage,root));
        root.setOnMouseExited(e-> root.setCursor(Cursor.DEFAULT));
        root.setOnMouseDragged((MouseEvent event) -> onMouseDragged(event,stage));
    }

    private static void onMouseMoved(MouseEvent event, Stage stage, BorderPane root){
        event.consume();
        double x = event.getSceneX();
        double y = event.getSceneY();
        double width = stage.getWidth();
        double height = stage.getHeight();
        Cursor cursorType;// 鼠标光标初始为默认类型，若未进入调整窗口状态，保持默认类型
        // 先将所有调整窗口状态重置
        isRight = isBottomRight = isBottom = false;
        if (y >= height - RESIZE_WIDTH) {
            if (x <= RESIZE_WIDTH) {
                cursorType = Cursor.DEFAULT;
            } else if (x >= width - RESIZE_WIDTH) {
                isBottomRight = true;
                cursorType = Cursor.SE_RESIZE;
            } else {// 下边界调整窗口状态
                isBottom = true;
                cursorType = Cursor.S_RESIZE;
            }
        } else if (x >= width - RESIZE_WIDTH) {
            isRight = true;
            cursorType = Cursor.E_RESIZE;
        }else{
            cursorType = Cursor.DEFAULT;
        }
        // 最后改变鼠标光标
        root.setCursor(cursorType);
    }

    private static void onMouseDragged(MouseEvent event, Stage stage){
        double x = event.getSceneX();
        double y = event.getSceneY();
        // 保存窗口改变后的x、y坐标和宽度、高度，用于预判是否会小于最小宽度、最小高度
        double nextX = stage.getX();
        double nextY = stage.getY();
        double nextWidth = stage.getWidth();
        double nextHeight = stage.getHeight();
        if (isRight || isBottomRight) {
            nextWidth = x;
        }
        if (isBottomRight || isBottom) {
            nextHeight = y;
        }
        if (nextWidth <= MIN_WIDTH) {
            nextWidth = MIN_WIDTH;
        }
        if (nextHeight <= MIN_HEIGHT) {
            nextHeight = MIN_HEIGHT;
        }
        stage.setX(nextX);
        stage.setY(nextY);
        stage.setWidth(nextWidth);
        stage.setHeight(nextHeight);
    }
}